package mfuny.ggongal.admin.http;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.json.JsonCreater;

import org.json.simple.JSONObject;

import com.ibatis.sqlmap.client.SqlMapClient;

public class HttpUserWeb {
	SqlMapClient sqlMapper;
	JsonCreater jc;
	
	public HttpUserWeb() throws Exception {
		sqlMapper = Server.getSqlMapper();
		jc = new JsonCreater();
	}
	
	//오늘의 경품게임 정보.
	@SuppressWarnings("unchecked")
	public JSONObject web_specialToday() throws Exception {
		JSONObject json = null;
		String day = new SimpleDateFormat("yyyy-M-d").format(new Date());
		List<Object> list = sqlMapper.queryForList("web_selectSGameInfo", day);
		if(list.size() > 0) {
			json = jc.getJsonByMapList(list, "sgameList");
			json.put("responseCode", 1);
		} else {
			json = new JSONObject();
			json.put("responseCode", 0);
		}
		
		return json;
	}
	
	//특정 경품게임의 랭킹(1~100).
	@SuppressWarnings("unchecked")
	public String web_getRank(int ad_no, String day, String hour, String callback) throws Exception {
		JSONObject json = null;
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		day += " " + hour + ":00:00";
		paramMap.put("ad_no", ad_no);
		paramMap.put("starttime", day);
		List<Object> rankList = sqlMapper.queryForList("web_getRankSpecial", paramMap);
		if(rankList.size() > 0) {
			json = jc.getJsonByMapList(rankList, "rankList");
			json.put("responseCode", 1);
		} else {
			json = new JSONObject();
			json.put("responseCode", 0);
		}
		
		String result = callback + "(" + json.toJSONString() + ")";
		
		return result;
	}
	
	//특정 유저 랭킹 검색.
	@SuppressWarnings("unchecked")
	public String web_getMyRank(String id, int ad_no, String day, String hour, String callback) throws Exception {
		JSONObject json = null;
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		day += " " + hour + ":00:00";
		paramMap.put("id", id);
		paramMap.put("ad_no", ad_no);
		paramMap.put("starttime", day);
		Integer myRank = (Integer)sqlMapper.queryForObject("web_getMyRank", paramMap);
		
		if( myRank == null ) {
			json = new JSONObject();
			json.put("responseCode", 0);
		} else {
			int startRank = myRank - 3 < 0 ? 0 : myRank - 3;
			paramMap.put("startRank", startRank);
			List<Object> list = sqlMapper.queryForList("web_getRankForMe", paramMap);
			if( list.size() > 0 ) {
				json = jc.getJsonByMapList(list, "myRankList");
				json.put("responseCode", 1);
				json.put("myRank", myRank);
			} else {
				json = new JSONObject();
				json.put("responseCode", 0);
			}
		}
		
		String result = callback + "(" + json.toJSONString() + ")";
		
		return result;
	}
}






































